"use strict";

/*************************************************************
SCRIPTS INDEX

ISOTOPE: INIT
ISOTOPE: GALLERY FILTER MENU
FLEXSLIDER INIT
FANCYBOX INIT
MAIN.JS
HOMEPAGE TWEETS
ADD NAV-PARENT CLASS TO NAV MENU ITEMS WITH SUBMENUS
BACK TO TOP
RESPONSIVE MENU
GALLERY FILTER MENU
SLIDES NAVIGATION ON HOVER ONLY
TOGGLE
ACCORDION
MASTHEAD BACKGROUND IMAGE
PARALLAX SCROLLING


*************************************************************/


/*************************************************************
ISOTOPE: INIT
*************************************************************/


	jQuery(window).load(function($) {

		$=jQuery;

		if ($('#thumb-gallery').size() > 0) {

			$('#thumb-gallery').isotope({
				itemSelector: '.gallery_item',
				layoutMode: 'fitRows'
			});
		}

	});


/*************************************************************
ISOTOPE: GALLERY FILTER MENU
*************************************************************/

	jQuery(document).ready(function($) {
		if ($('.filters li').size() > 0) {
			
			//apply selected class to first menu item (show all)
			$('.filters li:eq(1) a').addClass('selected');

			$('.filters li a').on('click', function (event) {
				event.preventDefault();
				var $this = $(this);

				//update selected filter item
				$('.filters li a').removeClass('selected');
				$this.addClass('selected');


				var filterVar = $this.closest('li').attr('class');
				if ( (typeof filterVar == 'undefined') || (filterVar.indexOf('cat-item-all') != -1) )  {
					filterVar = "*";
				} else {
					filterVar = filterVar.split(' ');
					filterVar = "." + filterVar[1];
				}
				$('#thumb-gallery').isotope({ filter: filterVar});

			});

		}
	});



/*************************************************************
FLEXSLIDER INIT
*************************************************************/

	jQuery(window).load(function($){
		$ = jQuery;

		if ($('.flexslider').size() > 0) {


			var canonAnimImgSliderSlidershow = (extData.canonOptionsAppearance['anim_img_slider_slideshow'] == 'checked') ? true : false;

			$('.flexslider').flexslider({
				slideshow: canonAnimImgSliderSlidershow,
				slideshowSpeed: parseInt(extData.canonOptionsAppearance['anim_img_slider_delay']),
				animationSpeed: parseInt(extData.canonOptionsAppearance['anim_img_slider_anim_duration']),
				animation: "fade",
				smoothHeight: true,
				touch: true,
				prevText: "S",
				nextText: "s",
				start: function(slider){
					$('body').removeClass('loading');
				}
			});

		}

		if (($('.flexslider-quote').size() > 0) && ($('#hp_tweets').size() === 0)) {

			var canonAnimQuoteSliderSlidershow = (extData.canonOptionsAppearance['anim_quote_slider_slideshow'] == 'checked') ? true : false;

			$('.flexslider-quote').flexslider({
				slideshow: canonAnimQuoteSliderSlidershow,
				slideshowSpeed: parseInt(extData.canonOptionsAppearance['anim_quote_slider_delay']),
				animationSpeed: parseInt(extData.canonOptionsAppearance['anim_quote_slider_anim_duration']),
				animation: "fade",
				smoothHeight: true,
				touch: true,
				directionNav: false,
				start: function(slider){
					$('body').removeClass('loading');
				}
			});	 

		}

	});


/*************************************************************
FANCYBOX INIT
*************************************************************/

	//attach fanybox class to all image links
	jQuery(document).ready(function($) {
		$("a[href$='.jpg'],a[href$='.png'],a[href$='.gif']").attr('rel','gallery').addClass('fancybox');
	});

	//init fancybox
	jQuery(document).ready(function($) {
		if ($(".fancybox").size() > 0) {
			var canonLightboxColor = extData.canonOptionsAppearance['lightbox_overlay_color'];
			var canonLightboxOpacity = extData.canonOptionsAppearance['lightbox_overlay_opacity'];;

			$(".fancybox").fancybox({
				helpers : {
			        overlay : {
			            css : {
			                'background' : jQuery.GlobalFunctions.hexOpacityToRgbaString(canonLightboxColor, canonLightboxOpacity)
			            }
			        }
			    }
			});
		}
	});



/*************************************************************
ADD NAV-PARENT CLASS TO NAV MENU ITEMS WITH SUBMENUS
*************************************************************/

	jQuery(document).ready(function($) {
		var $subMenus = $('.sub-menu');
		$subMenus.each(function(index) {
			var $this = $(this);
			$this.closest('li').addClass('nav-parent');
		});

	});


/*************************************************************
MAIN.JS
*************************************************************/

  	
	jQuery(document).ready(function($) {
  	
		// Parent-Nav Hover	
	  	$("li.nav-parent").hover(function(){
	  		$(this).addClass("hover");
	  	
	  	}, function(){
	  	    $(this).removeClass("hover");
	  	});
	  	
		// FitVid.js
		$(".main-container").fitVids();
	   
		// CleanTabs.js	
		$(".tab-container").cleanTabs(); 
	     
		// Mosaic.js		 
		$('.fade').mosaic();
		 
		// Superslides.js
		if ($('#slides').size() > 0) {

			var canonAnimSlider = (extData.canonOptionsAppearance['anim_slider'] == 'checked') ? true : false;
			$('#slides').superslides({
				play: canonAnimSlider,
				slide_easing: 'easeInOutCubic',
				hashchange: true,
				scrollable: true,
				delay: parseInt(extData.canonOptionsAppearance['anim_slider_delay']),
				slide_speed: parseInt(extData.canonOptionsAppearance['anim_slider_anim_duration']),
				pagination: true
			});
			
			$('#slides').on('dragstart', function (event) {
				event.preventDefault();
				switch(event.direction){
				 case "left":
				     $('#slides').superslides.api.next();
				     break;
				 case "right":
				     $('#slides').superslides.api.prev();
				     break;
				};

			});

		}
		 
	});



/*************************************************************
HOMEPAGE TWEETS
*************************************************************/

	jQuery(document).ready(function($) {
		if ($('#hp_tweets').size() > 0) {

			$('#hp_twitter_widget').hide();

			$(window).load(function() {
				$('#hp_twitter_widget iframe').each(function(index, e) {
					var $thisIframe = $(this);

					//set vars
					var success = false;
					var delay = 100;
					var attempts = 10;

					for (var $i = 1; $i < attempts+1; $i++) {  
								
						setTimeout(function() {
							if (success === false) {
							    if ($thisIframe.contents().find('.tweet').size() > 0) {
							    	success = true;	

									//get and post tweets
									var numTweets = extData.canonOptionsHp['hp_num_tweets'];
									var tweetCount = 0;
									$thisIframe.contents().find('.tweet').each(function(index, e){
										if (tweetCount == numTweets) return;
										var $this = $(this);
										var published = $this.find('time').text();
										var tweet = $this.find('.e-entry-title').html();
										var altTweet = "<li class='tweet'><blockquote>" + tweet + "<cite>- " + published + "</cite></blockquote></li>";
										
										var $associatedHpTweetsContainer = $thisIframe.closest('#hp_twitter_widget').next('#hp_tweets');
										$associatedHpTweetsContainer.find('ul').append(altTweet);
										tweetCount++;
									});

									//init flexslider-quote only when tweet slider markup has been built
									$('.flexslider-quote').flexslider({
										slideshow: true,
										animation: "fade",
										smoothHeight: true,
										touch: true,
										directionNav: false,
										start: function(slider){
											$('body').removeClass('loading');
										}
									});	

							    }
							}

						}, delay*$i);

					} // end fori

				});

			});
		}

	});



/*************************************************************
BACK TO TOP
*************************************************************/

	jQuery(document).ready(function($) {

		if ($('#to_top').size() > 0) {

			var $toTop = $('#to_top');

			$(window).scroll(function() {
				if($(this).scrollTop() != 0) {
					$('#to_top').fadeIn();	
				} else {
					$('#to_top').fadeOut();
				}
			});
		 
			$('#to_top').click(function() {
				$('body,html').animate({scrollTop:0},800);
			});	
			
		}

	});	

/*************************************************************
RESPONSIVE MENU
*************************************************************/

	jQuery(document).ready(function($) {

		// Responsive Menu.js
		$('#nav-wrap').prepend('<div id="menu-icon"><span><img src="'+ extData.templateURI +'/img/hamburger@2x.png"/></span>Menu</div>');
		$("#menu-icon").on("click", function(){
			$("#nav").slideToggle('medium', function() {
			    if ($('#nav').is(':visible'))
			        $('#nav').css('display','block');
			    if ($('#nav').is(':hidden'))
			        $('#nav').css('display','');    
			});
			
			$(this).toggleClass("active");
		});
		
	});


/*************************************************************
SLIDES NAVIGATION ON HOVER ONLY
*************************************************************/


	jQuery(document).ready(function($) {
		
		if ($('nav.slides-navigation').size() > 0) {

			var $slidesNavigation = $('nav.slides-navigation');
			$slidesNavigation.hide();

			//on masthead hover
			$('.slides-control').hover(function () {
				$slidesNavigation.fadeIn();
			}, function() {
				$slidesNavigation.hide();
			});

			//on masthead hover
			$slidesNavigation.hover(function () {
				$slidesNavigation.show();
			});

		}


	});

/*************************************************************
TOGGLE
*************************************************************/

	jQuery(document).ready(function(){

		if ($('.toggle-btn').size() > 0) {

			// toggle	  
			$('.toggle-btn').click(function(e){
				e.preventDefault();
				$(this).closest('li').find('.toggle-content').not(':animated').slideToggle();
			});
			
			// add active class for toggle
			$(".toggle-btn").click(function () {
				$(this).toggleClass("active");
			});

		}


	});

/*************************************************************
ACCORDION
*************************************************************/

	jQuery(document).ready(function(){

		// accordion	  
		$('.accordion-btn').click(function(e){
			e.preventDefault();
			var $this = $(this);
			var $thisAccordionContent = $this.closest('li').find('.accordion-content');
			var currentStatus = "";
			if ($this.attr('class').indexOf('active') != -1) {
				currentStatus = "active";
			}
			//first close all and remove active class
			$this.closest('.accordion').find('li').each(function(index) {
				var $thisLi = $(this);
				$thisLi.find('.accordion-btn').removeClass('active');
				$thisLi.find('.accordion-content').slideUp('400', function() {
					$(this).removeClass('active');
				});
			});
			if (currentStatus != "active") {
				$thisAccordionContent.not(':animated').slideDown();
				$this.addClass('active');
				$thisAccordionContent.addClass('active');
			}
		});
		
	});


/*************************************************************
MASTHEAD BACKGROUND IMAGE
*************************************************************/


	jQuery(document).ready(function($) {

		if ($('#static').size() > 0) {


			var mastheadImg = extData.templateURI + "/img/temp_masthead@2x.jpg";


			if ((typeof extData.canonOptionsSlider['slides'] != 'undefined') && (extData.canonOptionsSlider['slides'] != '')) {
				mastheadImg = extData.canonOptionsSlider['slides'][0]['image_url'];
			}

			// BackStretch.js	 
			$("#static").backstretch(mastheadImg);

		}
		 
	});



/*************************************************************
PARALLAX SCROLLING
*************************************************************/

	jQuery(document).ready(function($) {

		if ($('.header-container') && extData.canonOptions['parallax_scrolling'] == 'checked') {
			var parallaxScrollSpeed = parseInt(extData.canonOptions['parallax_scrolling_factor']); //the lower the number the more it moves

	        $(window).scroll(function() {
	            var yPos = -($(window).scrollTop() / parallaxScrollSpeed); 
	             
	            // Put together our final background position
	            var coords = '50% '+ yPos + 'px';
	 
	            // Move the background
	            //$('.header-container').css({ backgroundPosition: coords });
	            $('.header-container').css({ backgroundPosition: coords });
	        
	        }); 
		}
	});
